#! /usr/bin/python
import csv
import math
import os
import time
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import scipy.optimize


#fig = plt.figure()
#fig2 = plt.figure()
fig3 = plt.figure()
#fig3= plt.figure()

#plot_amp2 = fig.add_subplot(121)
#plot_amp3 = fig.add_subplot(122)
charge = fig3.add_subplot(111)


charge.set_title("Attenuation")
charge.set_xlabel("Distance from 2\" PMT (in)")
charge.set_ylabel("Mean Minimum Voltage")


x,xerr,y,yerr = np.loadtxt("fitdata",delimiter=' ',usecols=(0,1,2,3),unpack=True)
charge.errorbar(x,y,yerr,xerr,'-o')
#plt.show()


def exponential(xarg,a,b):
	return a*(math.e)**(b*xarg)
	
popt, pcov = scipy.optimize.curve_fit(exponential,x,y,p0=None,sigma=yerr)

print popt

func_x=np.linspace(x[0],x[len(x)-1],256,endpoint=True)
func_y=exponential(func_x,popt[0],popt[1])
charge.plot(func_x,func_y)

plt.show()
	


#histo_amptrig.hist(cut,50)
#plt.yscale('log', nonposy='clip')
#histo_amptrig.semilogy()
#voltagetime=sipm2.grab_single_waveform(1)

#print "mean:",np.mean(cut)
#print "err_mean:",np.std(cut)/math.sqrt(float(len(cut)))
#print "sigma:",np.std(cut)
#print "nentries:",len(cut)

#plt.show()

